﻿using System;
using Infrastructure.Interfaces;
using System.ServiceModel.DomainServices.Client;

namespace MVVMBasicRIALibrary
{
    public class BasicUnitOfWorkFactory : IUnitOfWorkFactory
    {
        private static Func<DomainContext> _domainContextDelegate;
        private static readonly Object _lockObject = new object();

        public static void SetDomainContext(Func<DomainContext> domainContextDelegate)
        {
            _domainContextDelegate = domainContextDelegate;
        }

        public IUnitOfWork Create()
        {
            DomainContext context;
            lock (_lockObject)
            {
                context = _domainContextDelegate();
            }
            return new BasicUnitOfWork(context);
        }
    }
}
